package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.StrategyCatalogQuery;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategyTravel")
public class StrategyTravelController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StrategyCatalogQuery qo,Long strategyId, Model model) {
        model.addAttribute("strategyIds", strategyService.listAll());
        model.addAttribute("strategyId", strategyId);
        List list = travelService.listAll();
        model.addAttribute("travel", list);
        if (strategyId!=null){
            PageInfo info = travelService.selectByStrategyId(qo);
            model.addAttribute("travels", info);
        }
        return "strategy/travel";
    }

    @RequestMapping("insertRelation")
    @ResponseBody
    public JSONResult insertRelation(Long strategyId, Long travelId) throws Exception {
        strategyService.insertRelation(strategyId, travelId);
        return new JSONResult();
    }

    @RequestMapping("deleteRelation")
    @ResponseBody
    public JSONResult deleteRelation(Long strategyId, Long travelId) throws Exception {
        this.strategyService.deleteRelation(strategyId, travelId);
        return new JSONResult();
    }

}
